package cn.bestick.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import cn.bestick.model.Grade;

/**
 * @author Bestick
 *
 *         be stick...
 *
 *         2015年9月8日 下午4:46:45
 */
public class GradeDao {

	/**
	 * 获取年级List
	 * 
	 * @param con
	 * @return
	 * @throws Exception
	 */
	public List<Grade> gradeList(Connection con) throws Exception {
		List<Grade> gradeList = new ArrayList<Grade>();
		String sql = "select * from t_grade";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while (rs.next()) {
			Grade grade = new Grade();
			grade.setGradeId(rs.getInt("gradeId"));
			grade.setGradeName(rs.getString("gradeName"));
			grade.setGradeDesc(rs.getString("gradeDesc"));
			gradeList.add(grade);
		}
		return gradeList;
	}
	
	/**
	 * 数据字典类别添加
	 * 
	 * @param con
	 * @param grade
	 * @return int
	 * @throws Exception
	 */
	public int gradeAdd(Connection con, Grade grade)
			throws Exception {
		String sql = "insert into t_grade values(null,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, grade.getGradeName());
		pstmt.setString(2, grade.getGradeDesc());
		return pstmt.executeUpdate();
	}

	/**
	 * 根据ddTypeId获取一个数据字典类别
	 * 
	 * @param con
	 * @param ddTypeId
	 * @return grade
	 * @throws Exception
	 */
	public Grade getGradeById(Connection con, int gradeId)
			throws Exception {
		Grade grade = null;
		String sql = "select * from t_grade where gradeId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, gradeId);
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()) {
			grade = new Grade();
			grade.setGradeId(rs.getInt("gradeId"));
			grade.setGradeName(rs.getString("gradeName"));
			grade.setGradeDesc(rs.getString("gradeDesc"));
		}
		return grade;
	}

	/**
	 * 对现有类别进行更新操作
	 * 
	 * @param con
	 * @param grade
	 * @return int
	 * @throws Exception
	 */
	public int gradeUpdate(Connection con, Grade grade)
			throws Exception {
		String sql = "update t_grade set gradeName=?,gradeDesc=? where gradeId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, grade.getGradeName());
		pstmt.setString(2, grade.getGradeDesc());
		pstmt.setInt(3, grade.getGradeId());
		return pstmt.executeUpdate();
	}

	/**
	 * 删除编号为ddTypeId的类别
	 * 
	 * @param con
	 * @param ddTypeId
	 * @return int
	 * @throws Exception
	 */
	public int gradeDelete(Connection con, int gradeId)
			throws Exception {
		String sql = "delete from t_grade where gradeId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, gradeId);
		return pstmt.executeUpdate();
	}
}
